/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managed;

import domen.Application;
import ejb.ApplicationFacade;
import ejb.UserFacade;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import managed.util.ImageUtil;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author Danilo
 */
@ManagedBean
@SessionScoped
public class UploadManagedBean {

    private Application application;
    private String filename;
    @EJB
    private ApplicationFacade af;
    @EJB
    private UserFacade uf;
    
    /** Creates a new instance of UploadManagedBean */
    public UploadManagedBean() {
        application = new Application();
    }
    
    public void uploadImage1(FileUploadEvent event){
        if(event.getFile().getFileName().contains(";")){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Not allowed type!"));
        }
        else{
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");  
        FacesContext.getCurrentInstance().addMessage(null, msg);    
        try{
            application.setImage1(ImageUtil.InputStreamToByte(ImageUtil.resizeImage(event.getFile().getInputstream())));
            } catch (IOException ex) {
                Logger.getLogger(UploadManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    public void uploadImage2(FileUploadEvent event){
        if(event.getFile().getFileName().contains(";")){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Not allowed type!"));
        }
        else{
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");  
        FacesContext.getCurrentInstance().addMessage(null, msg); 
        try {
                application.setImage2(ImageUtil.InputStreamToByte(ImageUtil.resizeImage(event.getFile().getInputstream())));
            } catch (IOException ex) {
                Logger.getLogger(UploadManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    public void uploadApk(FileUploadEvent event){
        if(event.getFile().getFileName().contains(";")){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error", "Not allowed type!"));
        }
        else{
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");  
        FacesContext.getCurrentInstance().addMessage(null, msg); 
        application.setApkFile(event.getFile().getContents());
        filename = event.getFile().getFileName();
        }
    }
    
    public void saveApk(){
        if(application.getApkFile()!=null && application.getImage1()!=null && application.getImage2()!=null){
        try{
        application.setUser(uf.find(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("id")));
        af.create(application);
        FacesMessage msg = new FacesMessage("Aplikacija je uspesno sacuvana!");  
        FacesContext.getCurrentInstance().addMessage(null, msg);
        application.setUser(uf.find(FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("id")));
        }catch(Exception ex){
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(ex.toString()));
        }
        reset();
        }
        else{
        FacesMessage msg = new FacesMessage("Greska!","Nisu svi fajlovi postavljeni!");  
        FacesContext.getCurrentInstance().addMessage(null, msg);
        
        reset();
        }
    }

    /**
     * @return the application
     */
    public Application getApplication() {
        return application;
    }

    /**
     * @param application the application to set
     */
    public void setApplication(Application application) {
        this.application = application;
    }

    /**
     * @return the image1
     */
    public StreamedContent getImage1() throws FileNotFoundException {
        if(application.getImage1()==null)
                 return new DefaultStreamedContent(FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/images/android.jpg"));
        return new DefaultStreamedContent(new ByteArrayInputStream(application.getImage1()));
    }

    /**
     * @return the image2
     */
    public StreamedContent getImage2() throws FileNotFoundException {
        if(application.getImage2()==null)
           return new DefaultStreamedContent(FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/images/android.jpg"));
        return new DefaultStreamedContent(new ByteArrayInputStream(application.getImage2()));
    }
    
    public String reset(){
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("uploadManagedBean");
        return "uploadApplication.xhtml?faces-redirect=true";
    }
    
    public String getFilename(){
        if(application.getApkFile()==null)
            filename = "";
        return filename;
    }
}
